package org.wutopia.examples.pathmappings;

import lombok.AllArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Arrays;
import java.util.Set;

@Component
@AllArgsConstructor
public class ServiceRegister {

    private RequestMappingHandlerMapping handlerMapping;

    @EventListener(ApplicationReadyEvent.class) // 应用完全就绪后触发
    public void registerToNacos() {
        System.out.println("向注册中心上报服务");


        handlerMapping.getHandlerMethods().forEach((info, method) -> {
            Set<String> patterns = info.getPatternValues();
            MethodParameter[] methodParameters = method.getMethodParameters();

            Arrays.stream(methodParameters).forEach(param -> {
                Class<?> type = param.getParameter().getType();
                System.out.println(type.getName());
            });
            System.out.println(method.getBeanType().getName());
            System.out.println(method.getMethod().getName());

            patterns.forEach(System.out::println); // 输出如 "/user", "/login"
        });
    }
}
